<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
    <style>
        .a2{
            width: 900px;
            height: 600px;
            border-radius: 20px;
            margin: 0 auto;
        }
    </style>
</head>
<body>
<div id="app">
    <el-form :inline="true" label-width="70px" :model="search">
        <el-form-item label="新闻类型">
            <el-input v-model="search.category"></el-input>
        </el-form-item>
        <el-button type="primary" @click="typeSearch()">搜索</el-button>
        <el-button @click="add()">添加</el-button>
        </el-form-item>
    </el-form>

<template>
    <el-table
            :data="typeArr"
            style="width: 100%">
        <el-table-column
                prop="id"
                label="编号"
                >
        </el-table-column>
        <el-table-column
                prop="category"
                label="新闻类型"
                >
        </el-table-column>
        <el-table-column label="操作" >
            <template slot-scope="scope">
                <el-button type="success" @click="get(scope.row.id)" >修改</el-button>
                <el-button type="danger" @click="deletess(scope.row.id)" disabled>删除</el-button>
            </template>
        </el-table-column>
    </el-table>
</template>
<el-pagination
        style="text-align: center; margin: 0 auto;"
        background
        @current-change="handleCurrentChange"
        layout="prev, pager, next"
        :total="totalCount">
</el-pagination>
    <el-dialog title="添加新闻类型" :visible.sync="dialogFormVisible" class="a2">
        <el-form :model="newsInfo" :rules="rules" ref="newsInfo" >
            <el-form-item label="新闻类型" prop="category">
                <el-input v-model="newsInfo.category" ></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addNews()">确 定</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改新闻类型" :visible.sync="dialogForm" class="a2">
        <el-form :model="newsInfo" :rules="rules" ref="newsInfo" >
            <el-form-item label="新闻类型" prop="category">
                <el-input v-model="newsInfo.category" ></el-input>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogForm= false">取 消</el-button>
            <el-button type="primary" @click="update()">确 定</el-button>
        </div>
    </el-dialog>
</div>
<script>
    var haha = new Vue({
        el: "#app",
        data: {
            search: {
                category: ""
            },
            typeArr: [],
            totalCount: 0,
            newsInfo: {},
            dialogFormVisible: false,
            dialogForm: false,
            rules: {
                category: [
                    {required: true, message: '请输入新闻类型', trigger: 'blur'},
                ]
            }
        },
        methods: {


            handleCurrentChange(val) {
                getData(val, 10);
            },

            addNews() {
                $.post("/ssm/category/add", haha.newsInfo, function (backData) {
                    if (backData.code == 1) {
                        haha.dialogFormVisible = false;
                        haha.$message.success(backData.msg);
                        getData(1, 10);
                        haha.$refs["newsInfo"].resetFields();

                    } else if (backData.code == 2) {
                        haha.$message.error('对不起 添加失败');
                    }
                });
            },


            add() {
                haha.dialogFormVisible = true;
            },


            get(id) {
                $.get("/ssm/category/get/" + id, function (backData) {
                    haha.newsInfo = backData.data;
                    haha.dialogForm = true;
                });
            },


            update() {
                $.post("/ssm/category/update", haha.newsInfo, function (backData) {
                    if (backData.code == 1) {
                        haha.dialogForm = false;
                        haha.$message.success('修改成功');
                        getData(1, 10);
                        haha.$refs["newsInfo"].resetFields();

                    } else if (backData.code == 2) {
                        haha.$message.error('对不起 修改失败');
                    }
                });
            },


            deletess(id){
                haha.$confirm('此操作将永久删除该新闻类型, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/ssm/category/delete/"+id,function (backData) {
                        haha.$message.success('删除成功');
                        getData(1,10);
                    });
                });
            },

        typeSearch() {
            getData(1, 10);
        }

       }

    });
    function getData(a , b) {
        haha.search.pageNo = a;
        haha.search.pageCount = b;
        //获取全部新闻类型，展示到表格中
        $.post("/ssm/category/queryType",haha.search,function (backData) {
            haha.typeArr = backData.data.currentData;
            haha.totalCount = backData.data.totalCount;
        });
    }
    getData(1,10);
</script>
</body>
</html>